package com.firebird.app.fekpsl.ui.activity.sword.friend

import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.firebird.app.fekpsl.R
import com.firebird.app.fekpsl.adapter.sword.friend.MyFollowAdapter
import com.firebird.app.fekpsl.base.BaseMvpSwipeBackActivity
import com.firebird.app.fekpsl.constant.Constant
import com.firebird.app.fekpsl.bean.BaseListResponseBody
import com.firebird.app.fekpsl.bean.Follow
import com.firebird.app.fekpsl.mvp.sword.friend.*
import com.firebird.app.fekpsl.utils.Preference
import com.firebird.app.fekpsl.widget.SpaceItemDecoration
import kotlinx.android.synthetic.main.fragment_common_refresh_layout.*
import kotlinx.android.synthetic.main.toolbar.*

class MyFollowActivity :  BaseMvpSwipeBackActivity<MyFollowContract.View, MyFollowContract.Presenter>(), MyFollowContract.View {
    /**
     * 每页数据的个数
     */
    private var pageSize = 10;
    var userUuid:String by Preference(Constant.UUID_KEY, "")
    /**
     * RecyclerView Divider
     */
    private val recyclerViewItemDecoration by lazy {
        SpaceItemDecoration(this)
    }

    private val myFollowAdapter: MyFollowAdapter by lazy {
        MyFollowAdapter()
    }

    /**
     * is Refresh
     */
    private var isRefresh = true

    override fun createPresenter(): MyFollowContract.Presenter = MyFollowPresenter()

    override fun attachLayoutRes(): Int = R.layout.sword_friend_my_follow

    override fun showLoading() {
        // swipeRefreshLayout.isRefreshing = isRefresh
    }

    override fun hideLoading() {
        swipeRefreshLayout?.isRefreshing = false
        if (isRefresh) {
            myFollowAdapter.setEnableLoadMore(true)
        }
    }

    override fun showError(errorMsg: String) {
        super.showError(errorMsg)
        if (isRefresh) {
            myFollowAdapter.setEnableLoadMore(true)
        } else {
            myFollowAdapter.loadMoreFail()
        }
    }

    override fun initData() {
    }

    override fun initView() {
        super.initView()
        toolbar.run {
            title = getString(R.string.my_follow)
            setSupportActionBar(this)
            supportActionBar?.setDisplayHomeAsUpEnabled(true)
        }
        swipeRefreshLayout.run {
            setOnRefreshListener(onRefreshListener)
        }
        recyclerView.run {
            layoutManager = LinearLayoutManager(this@MyFollowActivity)
            adapter = myFollowAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(recyclerViewItemDecoration)
        }
        myFollowAdapter.run {
            bindToRecyclerView(recyclerView)
            setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
        }
    }

    override fun start() {
        mPresenter?.getMyFollowList(0,userUuid)
    }

    override fun showMyFollowList(body: BaseListResponseBody<Follow>) {
        body.datas.let {
            myFollowAdapter.run {
                if (isRefresh) {
                    replaceData(it)
                } else {
                    addData(it)
                }
                pageSize = body.size
                if (body.over) {
                    loadMoreEnd(isRefresh)
                } else {
                    loadMoreComplete()
                }
            }
        }
    }

    /**
     * RefreshListener
     */
    private val onRefreshListener = SwipeRefreshLayout.OnRefreshListener {
        isRefresh = true
        myFollowAdapter.setEnableLoadMore(false)
        mPresenter?.getMyFollowList(0,userUuid)
    }
    /**
     * LoadMoreListener
     */
    private val onRequestLoadMoreListener = BaseQuickAdapter.RequestLoadMoreListener {
        isRefresh = false
        swipeRefreshLayout.isRefreshing = false
        val page = myFollowAdapter.data.size / pageSize + 1
        mPresenter?.getMyFollowList(page,userUuid)
    }

}
